<?php
/**
 +---------------------------------------<br/>
 * 控制器:<br/>
 +---------------------------------------
 * @category membership
 * @package web.front.action
 * @author skygreen skygreen2001@gmail.com
 */
class Action_Api extends Action
{
	/**
	 *卡支付
	 */
	public function kzf(){
		$usreid = HttpSession::get("userid");
        if (!$usreid) {
            $this->redirect("auth","login");
            return;
        }
		$this->loadCss("resources/css/kzf.css");
		$this->loadCss("resources/css/msg.css");
		$this->loadJs("js/msg.js");
        $this->loadJs("js/kzf.js");
        $store_id = HttpSession::get("store_id");
        $this->view->set("store_id",$store_id);
        $this->view->set("site_name","卡支付");
	}

	public function ajaxKzf(){
		$store_id = HttpSession::get("store_id");
		$data['cardno']= isset($_POST['cardno'])?$_POST['cardno']:"";
		$data['passwd']= isset($_POST['passwd'])?$_POST['passwd']:'';
		$data['saving_sub']= isset($_POST['saving_sub'])?$_POST['saving_sub']:0;
		$data['cash']= isset($_POST['cash'])?$_POST['cash']:0;
		$data['bank_amount']= isset($_POST['bank_amount'])?$_POST['bank_amount']:0;
		$data['point_sub']= isset($_POST['point_sub'])?$_POST['point_sub']:0;
		$data['coupon_pwd']= isset($_POST['coupon_pwd'])?$_POST['coupon_pwd']:'';
		$data['order_number'] = $store_id.date("YmdHis");
		$data['store_id'] = $store_id;
		$api = new UtilApi();
		$result = $api->trade($data);
		if ($result->code == "A00006") {
			$result2 = UtilApi::getMember($data['cardno']);
			if ($result2->code == "A00006") {
				$member = $result2->data;
			}
			$response = array(
                "success" => "success",
                "data" => $result->data,
                "member"=>$member,
                "url" => ""
            );
		}else{
			$response = array(
                "success" => "no",
                "data" => $result->result,
                "url" => ""
            );
		}
		echo json_encode($response);
        exit;
	}

	/**
	 *卡充值
	 */
	public function kcz(){
		$usreid = HttpSession::get("userid");
        if (!$usreid) {
            $this->redirect("auth","login");
            return;
        }
		$this->loadCss("resources/css/kcz.css");
		$this->loadCss("resources/css/msg.css");
		$this->loadJs("js/msg.js");
        $this->loadJs("js/kcz.js");
        $store_id = HttpSession::get("store_id");
        $this->view->set("store_id",$store_id);
        $this->view->set("site_name","卡充值");
	}

	public function ajaxKcz(){
		$passwrod= isset($_POST['password'])?$_POST['password']:"";
		if($passwrod != "888888"){
			$response = array(
                "success" => "no",
                "data" => "您输入的主管密码不正确",
                "url" => ""
            );
            echo json_encode($response);
        	exit;
		}
		$store_id = HttpSession::get("store_id");
		$data['cardno']= isset($_POST['cardno'])?$_POST['cardno']:"";
		$data['amount']= isset($_POST['amount'])?$_POST['amount']:0;
		$data['order_number'] = $store_id.date("YmdHis");
		$data['store_id'] = $store_id;
		$api = new UtilApi();
		$result = $api->charge($data);
		if ($result->code == "A00006") {
			$result2 = UtilApi::getMember($data['cardno']);
			if ($result2->code == "A00006") {
				$member = $result2->data;
			}
			$response = array(
                "success" => "success",
                "data" => "充值成功",
                "member"=>$member,
                "url" => ""
            );
		}else{
			$response = array(
                "success" => "no",
                "data" => $result->result,
                "url" => ""
            );
		}
		echo json_encode($response);
        exit;
	}

	/**
	 *卡激活
	 */
	public function kjh(){
		$usreid = HttpSession::get("userid");
        if (!$usreid) {
            $this->redirect("auth","login");
            return;
        }
		$this->loadCss("resources/css/kjh.css");
		$this->loadCss("resources/css/msg.css");
		$this->loadCss("resources/css/ui.thme.css");
		$this->loadJs("js/jquery.ui.core.js");
		$this->loadJs("js/jquery.ui.datepicker.js");
		$this->loadJs("js/msg.js");
        $this->loadJs("js/kjh.js");
        $store_id = HttpSession::get("store_id");
        $this->view->set("site_name","卡激活");
        $this->view->set("store_id",$store_id);
	}

	public function ajaxKjh(){
		$store_id = HttpSession::get("store_id");
		$data['card_no']= isset($_POST['cardno'])?$_POST['cardno']:"";
		$data['passwd']= isset($_POST['passwd'])?$_POST['passwd']:'';
		$data['mobile']= isset($_POST['mobile'])?$_POST['mobile']:'';
		$data['birthday']= isset($_POST['birthday'])?$_POST['birthday']:'';
		$data['store_id'] = $store_id;
		$api = new UtilApi();
		$result = $api->card_active($data);
		if ($result->code == "A00006") {
			HttpSession::set("user_id",$data['card_no']);
			$response = array(
                "success" => "success",
                "data" => "激活成功",
                "url" => ""
            );
		}else{
			$response = array(
                "success" => "no",
                "data" => $result->result,
                "url" => ""
            );
		}
		echo json_encode($response);
        exit;
	}

	/**
	 *购卡
	 */
	public function gk(){
		$usreid = HttpSession::get("userid");
        if (!$usreid) {
            $this->redirect("auth","login");
            return;
        }
		$this->loadCss("resources/css/gk.css");
		$this->loadCss("resources/css/msg.css");
		$this->loadJs("js/msg.js");
        $this->loadJs("js/gk.js");
        $this->view->set("site_name","购卡");
	}

	public function ajaxGk(){
		$store_id = HttpSession::get("store_id");
		$data['card_num']= isset($_POST['card_num'])?$_POST['card_num']:"";
		$data['order_number']= $store_id.date("YmdHis");
		$api = new UtilApi();
		$result = $api->buy_card_no($data);
		if ($result->code == "A00006") {

			$response = array(
                "success" => "success",
                "data" => "购买成功",
                "url" => ""
            );
		}else{
			$response = array(
                "success" => "no",
                "data" => $result->result,
                "url" => ""
            );
		}
		echo json_encode($response);
        exit;
	}

	/**
	 *卡挂失
	 */
	public function kgs(){
		$usreid = HttpSession::get("userid");
        if (!$usreid) {
            $this->redirect("auth","login");
            return;
        }
		$this->loadCss("resources/css/kgs.css");
		$this->loadCss("resources/css/msg.css");
		$this->loadJs("js/msg.js");
        $this->loadJs("js/kgs.js");
        $this->view->set("site_name","卡挂失");
	}

	public function ajaxKgs(){
		$store_id = HttpSession::get("store_id");
		$data['card_no']= isset($_POST['cardno'])?$_POST['cardno']:"";
		$data['password']= isset($_POST['password'])?$_POST['password']:'';
		$api = new UtilApi();
		$result = $api->report_lost($data);
		if ($result->code == "A00006") {
			$response = array(
                "success" => "success",
                "data" => "挂失成功",
                "url" => ""
            );
		}else{
			$response = array(
                "success" => "no",
                "data" => $result->result,
                "url" => ""
            );
		}
		echo json_encode($response);
        exit;
	}

	/**
	 *查看卡信息
	 */
	public function ckkxx(){
		$usreid = HttpSession::get("userid");
        if (!$usreid) {
            $this->redirect("auth","login");
            return;
        }
		$this->loadCss("resources/css/ckkxx.css");
		$this->loadCss("resources/css/msg.css");
		$this->loadCss("resources/css/ui.thme.css");
		$this->loadJs("js/jquery.ui.core.js");
		$this->loadJs("js/jquery.ui.datepicker.js");
		$this->loadJs("js/msg.js");
        $this->loadJs("js/ckkxx.js");
        $this->view->set("site_name","修改卡信息");
	}

	public function ajaxCkkxx(){
		$store_id = HttpSession::get("store_id");
		$cardno = isset($_POST['cardno'])?$_POST['cardno']:"";
		$api = new UtilApi();
		$result = $api->getMember($cardno);
		if ($result->code == "A00006") {
			$response = array(
                "success" => "success",
                "data" => $result->data,
                "url" => ""
            );
		}else{
			$response = array(
                "success" => "no",
                "data" => $result->result,
                "url" => ""
            );
		}
		echo json_encode($response);
        exit;
	}

	//修改会员信息
   public function ajaxCardedit(){
		$data['cardno']= isset($_POST['cardno'])?$_POST['cardno']:"";
		$data['passwd']= isset($_POST['passwd'])?$_POST['passwd']:"";
		$data['realname']= isset($_POST['realname'])?$_POST['realname']:"";
		$data['gender']= isset($_POST['gender'])?$_POST['gender']:"";
		$data['birthday']= isset($_POST['birthday'])?$_POST['birthday']:"";
		$data['idnum']= isset($_POST['idnum'])?$_POST['idnum']:"";
		$data['phone']= isset($_POST['phone'])?$_POST['phone']:"";
		$data['mobile']= isset($_POST['mobile'])?$_POST['mobile']:"";
		$data['email']= isset($_POST['email'])?$_POST['email']:"";
		$data['province']= isset($_POST['province'])?$_POST['province']:"";
		$data['city']= isset($_POST['city'])?$_POST['city']:"";
		$data['area']= isset($_POST['area'])?$_POST['area']:"";
		$data['address']= isset($_POST['address'])?$_POST['address']:"";
		$data['zipcode']= isset($_POST['zipcode'])?$_POST['zipcode']:"";
		$data['ext_ids']= isset($_POST['ext_ids'])?$_POST['ext_ids']:"";
		$data['ext_values']= isset($_POST['ext_values'])?$_POST['ext_values']:"";
		if ($data['cardno']=="" || $data['mobile']=="") {
			$response = array(
                "success" => "no",
                "data" => "卡号和者手机号不能为空",
                "url" => ""
            );
			echo json_encode($response);
        	exit;
		}
		$api = new UtilApi();
		$result = $api->modify_member($data);
		if ($result->code == "A00006") {
			$response = array(
                "success" => "success",
                "data" => "修改成功",
                "url" => ""
            );
		}else{
			$response = array(
                "success" => "no",
                "data" => $result->result,
                "url" => ""
            );
		}
		echo json_encode($response);
        exit;
   }

   /**
	 *添加会员
	 */
	public function tjhy(){
		$usreid = HttpSession::get("userid");
        if (!$usreid) {
            $this->redirect("auth","login");
            return;
        }
		$this->loadCss("resources/css/tjhy.css");
		$this->loadCss("resources/css/msg.css");
		$this->loadCss("resources/css/ui.thme.css");
		$this->loadJs("js/jquery.ui.core.js");
		$this->loadJs("js/jquery.ui.datepicker.js");
		$this->loadJs("js/msg.js");
        $this->loadJs("js/tjhy.js");
        $store_id = HttpSession::get("store_id");
        $this->view->set("site_name","添加会员");
        $this->view->set("store_id",$store_id);
	}

	public function ajaxTjhy(){
		$store_id = HttpSession::get("store_id");
		$data['realname']= isset($_POST['realname'])?$_POST['realname']:"";
		$data['password']= isset($_POST['password'])?$_POST['password']:"";
		$data['mobile']= isset($_POST['mobile'])?$_POST['mobile']:"";
		$data['email']= isset($_POST['email'])?$_POST['email']:"";
		$data['birthday']= isset($_POST['birthday'])?$_POST['birthday']:"";
		$data['gender']= isset($_POST['gender'])?$_POST['gender']:"";
		$api = new UtilApi();
		$result = $api->member_add($data);
		if ($result->code == "A00006") {
			$response = array(
                "success" => "success",
                "data" => "添加成功",
                "url" => ""
            );
		}else{
			$response = array(
                "success" => "no",
                "data" => $result->result,
                "url" => ""
            );
		}
		echo json_encode($response);
        exit;
	}

	/**
	 *查看交易
	 */
	public function ckjylsh(){
		$usreid = HttpSession::get("userid");
        if (!$usreid) {
            $this->redirect("auth","login");
            return;
        }
		$this->loadCss("resources/css/ckjylsh.css");
		$this->loadCss("resources/css/msg.css");
		$this->loadCss("resources/css/ui.thme.css");
		$this->loadJs("js/jquery.ui.core.js");
		$this->loadJs("js/jquery.ui.datepicker.js");
		$this->loadJs("js/msg.js");
        $this->loadJs("js/ckjylsh.js");
        $this->view->set("site_name","查看交易");
	}
	public function orderlist(){
		$usreid = HttpSession::get("userid");
        if (!$usreid) {
            $this->redirect("auth","login");
            return;
        }
		$this->loadCss("resources/css/orderlist.css");
		$this->loadJs("js/orderlist.js");
		$data['cardno']= isset($_POST['cardno'])?$_POST['cardno']:"";
		$data['type']= isset($_POST['type'])?$_POST['type']:"";
		$data['sdate']= isset($_POST['sdate'])?$_POST['sdate']:"";
		$data['edate']= isset($_POST['edate'])?$_POST['edate']:"";
		$data['page']= isset($_POST['page'])?$_POST['page']:1;
		$msg = "";
		if ($data['type']==1) {
			$site_name = "消费记录";
		}elseif ($data['type']==3){
			$site_name = "撤销支付";
		}elseif ($data['type']==4){
			$site_name = "撤销充值";
		}else{
			$site_name = "充值记录";
		}
		$api = new UtilApi();
		$result = $api->view_invoice($data);
		if ($result->code == "A00006") {
			$orderdata = $result->data;
			$count = $result->count;
		}else{
			$orderdata = array();
			$msg = $result->result;
			$count = 0;
		}
		$sum = ceil($count/10);
		$this->view->set("site_name",$site_name);
		$this->view->set("msg",$msg);
		$this->view->set("orderdata",$orderdata);
		$this->view->set("cardno",$data['cardno']);
		$this->view->set("type",$data['type']);
		$this->view->set("sdate",$data['sdate']);
		$this->view->set("edate",$data['edate']);
		$this->view->set("page",$data['page']);
		$this->view->set("sum",$sum);
		$pages = array();
		if($sum >0){
			for ($i=1; $i<=$sum ; $i++) { 
				$pages[]=$i;
			}
		}
		$this->view->set("pages",$pages);
	}

	/**
	 *查看日志
	 */
	public function log(){
		$usreid = HttpSession::get("userid");
        if (!$usreid) {
            $this->redirect("auth","login");
            return;
        }
		$this->loadCss("resources/css/log.css");
		$this->loadCss("resources/css/msg.css");
		$this->loadCss("resources/css/ui.thme.css");
		$this->loadJs("js/jquery.ui.core.js");
		$this->loadJs("js/jquery.ui.datepicker.js");
		$this->loadJs("js/msg.js");
        $this->loadJs("js/log.js");
        $this->view->set("site_name","查看日志");
	}
	public function loglist(){
		$usreid = HttpSession::get("userid");
        if (!$usreid) {
            $this->redirect("auth","login");
            return;
        }
		$this->loadCss("resources/css/orderlist.css");
		$this->loadJs("js/orderlist.js");
		$data['cardno']= isset($_POST['cardno'])?$_POST['cardno']:"";
		$store_id = HttpSession::get("store_id");
		$data['store_id']=$store_id;
		$data['type']= isset($_POST['type'])?$_POST['type']:"";
		$data['sdate']= isset($_POST['sdate'])?$_POST['sdate']:"";
		$data['edate']= isset($_POST['edate'])?$_POST['edate']:"";
		$data['page']= isset($_POST['page'])?$_POST['page']:1;
		$msg = "";
		
		$api = new UtilApi();
		$result = $api->get_card_opt($data);
		if ($result->code == "A00006") {
			$logdata = $result->data;
			$count = $result->count;
		}else{
			$logdata = array();
			$msg = $result->result;
			$count = 0;
		}
		$sum = ceil($count/10);
		$this->view->set("site_name","日志列表");
		$this->view->set("msg",$msg);
		$this->view->set("logdata",$logdata);
		$this->view->set("cardno",$data['cardno']);
		$this->view->set("type",$data['type']);
		$this->view->set("sdate",$data['sdate']);
		$this->view->set("edate",$data['edate']);
		$this->view->set("page",$data['page']);
		$this->view->set("sum",$sum);
		$pages = array();
		if($sum >0){
			for ($i=1; $i<=$sum ; $i++) { 
				$pages[]=$i;
			}
		}
		$this->view->set("pages",$pages);
	}

	/**
	 *卡转账
	 */
	public function kzz(){
		$usreid = HttpSession::get("userid");
        if (!$usreid) {
            $this->redirect("auth","login");
            return;
        }
		$this->loadCss("resources/css/kzz.css");
		$this->loadCss("resources/css/msg.css");
		$this->loadJs("js/msg.js");
        $this->loadJs("js/kzz.js");
        $this->view->set("site_name","卡转账");
	}

	public function ajaxKzz(){
		$store_id = HttpSession::get("store_id");
		$data['from_card_no']= isset($_POST['cardno'])?$_POST['cardno']:"";
		$data['passwd']= isset($_POST['password'])?$_POST['password']:'';
		$data['to_card_no']= isset($_POST['to_card_no'])?$_POST['to_card_no']:'';
		$data['user_balance']= isset($_POST['user_balance'])?$_POST['user_balance']:'';
		$data['order_number']= $store_id.date("YmdHis");
		$data['store_id']= $store_id;
		$api = new UtilApi();
		$result = $api->transfer($data);
		if ($result->code == "A00006") {
			$response = array(
                "success" => "success",
                "data" => "转账成功",
                "url" => ""
            );
		}else{
			$response = array(
                "success" => "no",
                "data" => $result->result,
                "url" => ""
            );
		}
		echo json_encode($response);
        exit;
	}

	/**
	 *卡修改密码
	 */
	public function xgmm(){
		$usreid = HttpSession::get("userid");
        if (!$usreid) {
            $this->redirect("auth","login");
            return;
        }
		$this->loadCss("resources/css/xgmm.css");
		$this->loadCss("resources/css/msg.css");
		$this->loadJs("js/msg.js");
        $this->loadJs("js/xgmm.js");
        $this->view->set("site_name","卡修改密码");
	}

	public function ajaxXgmm(){
		$store_id = HttpSession::get("store_id");
		$data['card_no']= isset($_POST['cardno'])?$_POST['cardno']:"";
		$data['oldpassword']= isset($_POST['oldpassword'])?$_POST['oldpassword']:'';
		$data['newpassword']= isset($_POST['newpassword'])?$_POST['newpassword']:'';

		$api = new UtilApi();
		$result = $api->update_password($data);
		if ($result->code == "A00006") {
			$response = array(
                "success" => "success",
                "data" => "密码修改成功",
                "url" => ""
            );
		}else{
			$response = array(
                "success" => "no",
                "data" => $result->result,
                "url" => ""
            );
		}
		echo json_encode($response);
        exit;
	}
	/**
	 *换卡
	 */
	public function hk(){
		$usreid = HttpSession::get("userid");
        if (!$usreid) {
            $this->redirect("auth","login");
            return;
        }
		$this->loadCss("resources/css/hk.css");
		$this->loadCss("resources/css/msg.css");
		$this->loadJs("js/msg.js");
        $this->loadJs("js/hk.js");
        $this->view->set("site_name","换卡");
	}
	public function ajaxHk(){
		$store_id = HttpSession::get("store_id");
		$userid = HttpSession::get("userid");
		$member = UtilApi::get_store($userid);
		$oldcard = isset($_POST['oldcard'])?$_POST['oldcard']:"";
		$newcard = isset($_POST['newcard'])?$_POST['newcard']:"";
		$oldpwd = isset($_POST['oldpwd'])?$_POST['oldpwd']:"";
		$newpwd = isset($_POST['newpwd'])?$_POST['newpwd']:"";
		$data['store_id'] = $store_id;
        $data['emp_no'] = $member->data->realname;
        $data['old_card'] = $oldcard; //被补办的卡号
        $data['new_card'] = $newcard; //补办的卡号
        $data['password'] = $oldpwd; //原始卡密码
        $data['new_password'] = $newpwd; //新密码
		$api = new UtilApi();
		$result = $api->replace_card($data);
		if ($result->code == "A00006") {
			$response = array(
                "success" => "success",
                "data" => $result->result,
                "url" => ""
            );
		}else{
			$response = array(
                "success" => "no",
                "data" => $result->result,
                "url" => ""
            );
		}
		echo json_encode($response);
        exit;
	}
	/**
	 *撤销交易
	 */
	public function cancel(){
		$usreid = HttpSession::get("userid");
        if (!$usreid) {
            $this->redirect("auth","login");
            return;
        }
		$this->loadCss("resources/css/cancel.css");
		$this->loadCss("resources/css/msg.css");
		$this->loadJs("js/msg.js");
        $this->loadJs("js/cancel.js");
        $this->view->set("site_name","撤销交易");
	}

	public function ajaxCancel(){
		$store_id = HttpSession::get("store_id");
		$invoice_id = isset($_POST['invoice_id'])?$_POST['invoice_id']:"";
		$data['invoice_id']=$invoice_id;
		$data['store_id']=$store_id;
		$api = new UtilApi();
		$result = $api->trade_cancel($data);
		if ($result->code == "A00006") {
			$response = array(
                "success" => "success",
                "data" => "",
                "url" => ""
            );
		}else{
			$response = array(
                "success" => "no",
                "data" => $result->result,
                "url" => ""
            );
		}
		echo json_encode($response);
        exit;
	}

	/**
	 *报表
	 */
	public function totalTable(){
		$usreid = HttpSession::get("userid");
        if (!$usreid) {
            $this->redirect("auth","login");
            return;
        }
		$this->loadCss("resources/css/totalTable.css");
		$this->loadCss("resources/css/msg.css");
		$this->loadCss("resources/css/ui.thme.css");
		$this->loadJs("js/jquery.ui.core.js");
		$this->loadJs("js/jquery.ui.datepicker.js");
		$this->loadJs("js/msg.js");
        $this->loadJs("js/totalTable.js");
        $this->view->set("site_name","报表");
	}
	public function totalList(){
		$store_id = HttpSession::get("store_id");
		$sdate = isset($_POST['sdate'])?$_POST['sdate']:"";
		$edate = isset($_POST['edate'])?$_POST['edate']:"";
		
		$data['store_id']=$store_id;
		$data['sdate']=$sdate;
		$data['edate']=$edate;
		$api = new UtilApi();
		$result = $api->app_total($data);
		
		$this->loadCss("resources/css/totalTable.css");
		$this->loadCss("resources/css/msg.css");
		$this->loadJs("js/msg.js");
        $this->loadJs("js/totalTable.js");
        $this->view->set("site_name","报表");
        $this->view->set("totalList",$result);
	}

	public function msm(){
		$usreid = HttpSession::get("userid");
        if (!$usreid) {
            $this->redirect("auth","login");
            return;
        }
        $this->loadCss("resources/css/msm.css");
		$this->loadCss("resources/css/msg.css");
		$this->loadJs("js/msg.js");
        $this->loadJs("js/sendmsm.js");
        $this->view->set("site_name","发送短信");
	}

	//发送短信
	public function sendSms(){
		$store_id = HttpSession::get("store_id");
		$mobile = isset($_POST['mobile'])?$_POST['mobile']:"";
		$contents = isset($_POST['contents'])?$_POST['contents']:"";
		$data['mobile']=$mobile;
		$data['contents']=$contents;
		$api = new UtilApi();
		$result = $api->send_sms($data);
		if ($result->code == "A00006") {
			$response = array(
                "success" => "success",
                "data" => $result->msg,
                "url" => ""
            );
		}else{
			$response = array(
                "success" => "no",
                "data" => $result->msg,
                "msg"=>$data['contents'],
                "url" => ""
            );
		}
		echo json_encode($response);
        exit;
	}

	public function sendWx(){

	}
}